Page({
  data: {
    orderId: '',
    order: {
      id: 1,
      serviceName: '全身放松按摩',
      serviceImage: '/assets/images/service1.jpg',
      technicianName: '张技师',
      price: 198.00
    },
    selectedMethod: 'wechat',
    wallet: {
      balance: 150.00
    }
  },

  onLoad: function(options) {
    if (options.orderId) {
      this.setData({
        orderId: options.orderId
      });
      this.loadOrderDetail(options.orderId);
      this.loadWalletInfo();
    }
  },

  loadOrderDetail: function(orderId) {
    const app = getApp();
    
    app.request({
      url: `/orders/${orderId}/`,
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            order: res.data
          });
        }
      }
    });
  },

  loadWalletInfo: function() {
    const app = getApp();
    
    app.request({
      url: '/wallet/',
      success: (res) => {
        if (res.statusCode === 200) {
          this.setData({
            wallet: res.data
          });
        }
      }
    });
  },

  selectMethod: function(e) {
    const method = e.currentTarget.dataset.method;
    this.setData({
      selectedMethod: method
    });
  },

  confirmPayment: function() {
    if (this.data.selectedMethod === 'wechat') {
      this.wechatPay();
    } else if (this.data.selectedMethod === 'balance') {
      this.balancePay();
    }
  },

  wechatPay: function() {
    const app = getApp();
    
    // 调用后端微信支付接口
    app.request({
      url: '/payments/wechat/pay/',
      method: 'POST',
      data: {
        order_id: this.data.orderId
      },
      success: (res) => {
        if (res.statusCode === 200) {
          // 调用微信支付
          const payParams = res.data.pay_params;
          
          wx.requestPayment({
            timeStamp: payParams.timeStamp,
            nonceStr: payParams.nonceStr,
            package: payParams.package,
            signType: payParams.signType,
            paySign: payParams.paySign,
            success: (result) => {
              // 支付成功
              wx.showToast({
                title: '支付成功',
                icon: 'success'
              });
              
              // 跳转到订单详情页
              setTimeout(() => {
                wx.redirectTo({
                  url: `/pages/customer/orderDetail?id=${this.data.orderId}`
                });
              }, 1500);
            },
            fail: (error) => {
              // 支付失败
              wx.showToast({
                title: '支付取消',
                icon: 'none'
              });
            }
          });
        } else {
          wx.showToast({
            title: res.data.error || '支付请求失败',
            icon: 'none'
          });
        }
      },
      fail: (error) => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  },

  balancePay: function() {
    // 余额支付逻辑
    if (this.data.wallet.balance < this.data.order.price) {
      wx.showToast({
        title: '余额不足',
        icon: 'none'
      });
      return;
    }
    
    const app = getApp();
    
    app.request({
      url: '/payments/balance/pay/',
      method: 'POST',
      data: {
        order_id: this.data.orderId
      },
      success: (res) => {
        if (res.statusCode === 200) {
          wx.showToast({
            title: '支付成功',
            icon: 'success'
          });
          
          // 跳转到订单详情页
          setTimeout(() => {
            wx.redirectTo({
              url: `/pages/customer/orderDetail?id=${this.data.orderId}`
            });
          }, 1500);
        } else {
          wx.showToast({
            title: res.data.error || '支付失败',
            icon: 'none'
          });
        }
      }
    });
  }
})